<?php
namespace app\http;

use app\common\model\Compute;
use app\common\model\ComputeDetail;
use app\common\model\Dose;
use app\common\model\Meal;
use app\common\model\Menu;
use app\common\model\Order;
use app\common\model\Quantify;
use app\common\model\Type;
use think\facade\Config;
//require_once '/vendor/autoload.php';
// 导入要请求接口对应的Request类
// 导入可选配置类
use think\worker\Server;
use Workerman\Lib\Timer;

class Dingliang extends Server
{
                                                    //protected $processes = 4;
    protected $socket = 'websocket://0.0.0.0:2004'; //正式
                                                    //protected $socket = 'tcp://0.0.0.0:2008'; //测试
    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {

        Timer::add(1, [$this, 'compute']);

    }
    public function compute()
    {
        echo '[定量计算]--开始运行';
        echo "\r\n";
        $week = date('w');
        if ($week == 0) {
            $week = 7;
        }
        $config    = Config::get('site.site');
        $start_end = getstart();
        $start     = $start_end['start'];
        $end       = $start_end['end'];
        $seting    = get_database_setting('site');

        if ($seting['site']['order'] == 0) {

            //if ($week == $config['week']) {
            $order_list = Order::where('status', 0)->select();
            $meal_list  = [];
            $type_list  = [];
            foreach ($order_list as $order_key => $order_value) {
                $dose_list = Dose::where('order_id', $order_value['id'])->where('week', 'between', [$start, $end])->select();
                foreach ($dose_list as $dose_key => $dose_value) {

                    foreach ($dose_value['detail'] as $detail_key => $detail_value) {

                        $meal_list[] = [
                            'meal_id'   => $detail_value['meal_id'],
                            'num'       => $detail_value['num'],
                            'school_id' => $order_value['school_id'],
                            'order_id'  => $order_value['id'],
                            'dose_id'   => $dose_value['id'],
                            'tag'       => $dose_value['tag'],
                        ];
                    }

                    foreach ($dose_value['detail2'] as $detail2_key => $detail2_value) {
                        if (! isset($detail2_value['num'])) {
                            $detail2_value['num'] = 0;
                        }
                        if ($detail2_value['brand_id'] == 22 && $detail2_value['spec_id'] == 214) {
                            $detail2_num      = round($detail2_value['num'] / 500, 2);
                            $detail2_spec_id  = 4;
                            $detail2_brand_id = 3;
                        } else {
                            $detail2_num      = $detail2_value['num'];
                            $detail2_spec_id  = $detail2_value['spec_id'];
                            $detail2_brand_id = $detail2_value['brand_id'];
                        }
                        $type_list[] = [
                            'type_id'   => $detail2_value['type_id'],
                            'spec_id'   => $detail2_spec_id,
                            'brand_id'  => $detail2_brand_id,
                            'num'       => $detail2_num,
                            'school_id' => $order_value['school_id'],
                            'order_id'  => $order_value['id'],
                            'dose_id'   => $dose_value['id'],
                        ];
                    }

                }
                Order::where('id', $order_value['id'])->update(['status' => 1]);
            }

            $menu_list = [];
            if (count($order_list) > 0) {

                foreach ($meal_list as $meal_key => $meal_value) {

                    $meal = Meal::where('id', $meal_value['meal_id'])->findOrEmpty();
                    if (! $meal->isEmpty()) {
                        foreach ($meal['detail'] as $meal_detail_key => $meal_detail_value) {
                            $menu_list[] = $meal_detail_value['menu_id'];
                            $menu        = Menu::where('id', $meal_detail_value['menu_id'])->findOrEmpty();
                            if (! $menu->isEmpty()) {
                                foreach ($menu['detail'] as $menu_detail_key => $menu_detail_value) {
                                    if ($meal_value['num'] == '') {
                                        $meal_value['num'] = 0;
                                    }
                                    if ($meal_value['tag'] == 1) {
                                        $meal_value['num'] = 1;
                                    }

                                    if (! isset($menu_detail_value['num']) || $menu_detail_value['num'] == '') {
                                        $menu_detail_value['num'] = 0;
                                    }
                                    if (! is_numeric($menu_detail_value['num'])) {
                                        $menu_detail_value['num'] = 0;
                                    }
                                    if ($menu_detail_value['brand_id'] == 22 && $menu_detail_value['spec_id'] == 214) {

                                        $menu_detail_num      = round(($meal_value['num'] * $menu_detail_value['num']) / 500, 2);
                                        $menu_detail_spec_id  = 4;
                                        $menu_detail_brand_id = 3;

                                    } else {

                                        $menu_detail_num      = round($meal_value['num'] * $menu_detail_value['num'], 2);
                                        $menu_detail_spec_id  = $menu_detail_value['spec_id'];
                                        $menu_detail_brand_id = $menu_detail_value['brand_id'];

                                    }
                                    if ($menu_detail_value['type_id'] == 197 || $menu_detail_value['type_id'] == 7) {
                                        $menu_detail_value['type_id'] = 410;
                                    }
                                    if ($menu_detail_value['type_id'] == 196) {
                                        $menu_detail_value['type_id'] = 411;
                                    }
                                    if ($menu_detail_value['type_id'] == 195) {
                                        $menu_detail_value['type_id'] = 409;
                                    }
                                    $type_list[] = [
                                        'type_id'   => $menu_detail_value['type_id'],
                                        'num'       => $menu_detail_num,
                                        'spec_id'   => $menu_detail_spec_id,
                                        'brand_id'  => $menu_detail_brand_id,
                                        'school_id' => $meal_value['school_id'],
                                        'order_id'  => $meal_value['order_id'],
                                        'dose_id'   => $meal_value['dose_id'],
                                    ];
                                }
                            }
                        }
                    }
                }

                $type_list2 = $this->array_do($type_list);

                $type_list3 = $this->array_do2($type_list);

                $start_end = getstart();
                $start     = $start_end['start'];
                $end       = $start_end['end'];
                $quantify  = Quantify::create(['name' => $start . '-' . $end . '计算结果']);
                foreach ($type_list2 as $key => $value) {
                    $type = Type::where('id', $value['type_id'])->findOrEmpty();
                    if (! $type->isEmpty()) {
                        $compute_data = [
                            'quantify_id' => $quantify->id,
                            'parent_id'   => $type['parent_id'],
                            'type_id'     => $value['type_id'],
                            'spec_id'     => $value['spec_id'],
                            'brand_id'    => $value['brand_id'],
                            'code'        => date('YmdHis') . rand(00000, 99999),
                            'all_num'     => $value['num'],
                        ];
                        $compute = Compute::create($compute_data);
                        foreach ($type_list3 as $tk => $tv) {

                            $compute_detail_data = [
                                'user_id'    => 0,
                                'compute_id' => $compute->id,
                                'order_id'   => $tv['order_id'],
                                'school_id'  => $tv['school_id'],
                                'type_id'    => $tv['type_id'],
                                'spec_id'    => $tv['spec_id'],
                                'brand_id'   => $tv['brand_id'],
                                'num'        => $tv['num'],
                                'dose_id'    => $tv['dose_id'],
                            ];
                            $compute_detail = ComputeDetail::create($compute_detail_data);
                        }
                    }
                }
            }
        }

    }

    public function array_do($array)
    {
        $result = [];

        foreach ($array as $item) {
            $type_id = $item['type_id'];
            if (! isset($result[$type_id])) {
                $result[$type_id] = $item;
            } else {
                $result[$type_id]['num'] += $item['num'];
            }
        }

        $result = array_values($result);
        return $result;
    }
    public function array_do2($data)
    {
        $groupedData = [];

        foreach ($data as $item) {
            $school_id = $item['school_id'];
            $type_id   = $item['type_id'];
            $dose_id   = $item['dose_id'];
            $brand_id  = $item['brand_id'];
            $order_id  = $item['order_id'];
            $key       = $school_id . '-' . $type_id . '-' . $dose_id;

            if (! isset($groupedData[$key])) {
                $groupedData[$key] = $item;
            } else {
                $groupedData[$key]['num'] += $item['num'];
            }

        }
        $result = array_values($groupedData);
        return $result;
    }

}
